#!/usr/bin/env bash
function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
echo "HTTP/1.1 200 OK"
echo "Content-Type: text/plain"
echo "Connection: close"
echo 

while read -t 0.01 line; do
    line=${line//$'\r'}
    if [ -z "$line" ]; then break; fi
    if echo "${line}" | grep -qi "^GET\|POST\|PUT\|DELETE"; then
      # GET /test123?r=123 HTTP/1.1
      export HTTP_REQUEST="${line}"
      export HTTP_REQ_METHOD="$(echo "${line}"|cut -d" " -f 1)"
      export HTTP_REQ_URI="$(echo "${line}"|cut -d" " -f 2)"
      export HTTP_REQ_URI_PATH="$(echo "${HTTP_REQ_URI}"|cut -d"?" -f 1)"
      if echo "$HTTP_REQ_URI"|grep -q '?'; then
        export HTTP_REQ_URI_PARAMS="$(echo "${HTTP_REQ_URI}"|cut -d"?" -f 2-)"
      else
	export HTTP_REQ_URI_PARAMS=""
      fi
      export HTTP_REQ_VERSION="$(echo "${line}"|cut -d" " -f 3-)"
    elif echo "${line}" | grep -qi "^User-Agent:"; then
      # User-Agent: curl/7.29.0
      export HTTP_USER_AGENT="$(echo "${line}"|cut -d" " -f 2-)"
    elif echo "${line}" | grep -qi "^Host:"; then
      # Host: 0.0.0.0:8081
      export HTTP_SERVER="$(echo "${line}"|cut -d" " -f 2-)"
    elif echo "${line}" | grep -qi "^Accept:"; then
      # Accept: */*
      export HTTP_ACCEPT="$(echo "${line}"|cut -d" " -f 2-)"
      #continue
    elif echo "${line}" | grep -qi "^Content-Length:"; then
      # Content-Length: 5
      export HTTP_CONTENT_LENGTH="$(echo "${line}"|cut -d" " -f 2-)"
    elif echo "${line}" | grep -qi "^Content-Type:"; then
      # Content-Type: application/x-www-form-urlencoded
      export HTTP_CONTENT_TYPE="$(echo "${line}"|cut -d" " -f 2-)"
    elif echo "${line}" | grep -qi "^X-Haproxy-Server-State:"; then
      # X-Haproxy-Server-State: UP; etc..
      export HTTP_HAPROXY_SERVER_STATE="$(echo "${line}"|cut -d" " -f 2-)"
    elif [ ${#line} -ge 1 ]; then
      # <any header>
      continue
    else
      break
      #continue
    fi
done

if [ "${HTTP_REQ_METHOD}" == "POST" ] && [ ${HTTP_CONTENT_LENGTH} -ge 1 ]; then
export HTTP_POST_CONTENT=""
  DATA_LENGTH=$HTTP_CONTENT_LENGTH
  # If the value of Content-Length is greater than the actual content, then
  # read will timeout and never allow the collection from standard input.
  # This is overcome by reading one character at a time.
  #READ_BUFFER_LENGTH=1
  # If you are sure the value of Content-Length always equals the length of the
  # content, then all of standard input can be read in at one time
  READ_BUFFER_LENGTH=$DATA_LENGTH
  #
  # Read POST data via standard input
  while IFS= read -n $READ_BUFFER_LENGTH -r -t 0.01 post_buffer; do
    let "DATA_LENGTH = DATA_LENGTH - READ_BUFFER_LENGTH"
    HTTP_POST_CONTENT="${HTTP_POST_CONTENT}${post_buffer}"
    # Stop reading if we reach the content length, max length, or expected length
    if [ ${#HTTP_POST_CONTENT} -ge ${HTTP_CONTENT_LENGTH} ]; then
      break;
    elif [ ${#HTTP_POST_CONTENT} -ge ${MAX_HTTP_POST_LENGTH} ]; then
      break;
    elif [ ${DATA_LENGTH} -le 0 ]; then
      break;
    fi
  done

  urldecode "${HTTP_POST_CONTENT}"
fi
echo ${HTTP_REQ_URI}
echo ${HTTP_REQ_PARAMS}
echo $(urldecode ${HTTP_REQ_URI_PARAMS})

exit 0
